<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class user extends check{
	//构造函数
	public function __construct(){
		parent::__construct();
		$this->ucat = spClass('m_ucat')->findAll(null, 'id ASC');
		$this->jmxz = array(1=>'地区总代理', 2=>'代理', 3=>'旗舰店', 4=>'标准店', 5=>'普通店', 6=>'专柜', 7=>'代理商再招商');
		$this->jyzk = array(1=>'正常', 2=>'一般', 3=>'不进货', 4=>'停业', 5=>'合同终止');
		$this->khdj = array(1=>'A类', 2=>'B类', 3=>'C类');
		$this->xsqk = array(1=>'好', 2=>'一般', 3=>'不好', 4=>'不做');
	}
	//用户列表
	public function lis(){
		$where = ' 1 = 1 ';
		if($search = $this->spArgs('search')){
			if($search['groupid'] != ''){
				$where .= " AND groupid = {$search[groupid]} ";
			}
			if($usercode = trim($search['usercode'])){
				$usercode_s = spClass('m_user')->escape('%' . $usercode . '%');
				$where .= " AND usercode LIKE {$usercode_s} ";
				$search['usercode'] = $usercode;
			}
			$this->search = $search;
		}else{
			if($groupid = $this->spArgs('groupid')){
				$where .= " AND groupid = {$groupid} ";
			}
			if($usercode = $this->spArgs('usercode')){
				$usercode = urldecode($usercode);
				$where .= " AND usercode LIKE '%{$usercode}%' ";
			}
			$this->search = array('groupid'=>$groupid,'usercode'=>$usercode);
		}
		$page = (int) $this->spArgs('page',1) > 1 ? (int) $this->spArgs('page',1) : 1;
		$this->user = spClass('m_user')->spLinker()->spPager($page,15)->findAll($where,'id DESC');
		$this->pager = spClass('m_user')->spPager()->getPager();
		$this->display('user_lis.tpl.php');		
	}
	//添加用户
	public function add(){
		$this->display('user_add.tpl.php');
	}
	public function doadd(){
		if(false != spClass('m_user')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('user','add'));
		$rows = $this->spArgs();
		if(isset($rows['password2'])) unset($rows['password2']);
		$rows['password'] = md5($rows['password']);
		$rows['time'] = time();
		$rows['kysj'] = strtotime($rows['kysj']);
		if(false != spClass('m_user')->create($rows)){
			$this->success('添加会员成功！',spUrl('user','lis'));
		}else{
			$this->error('添加会员失败！',spUrl('user','add'));
		}
	}
	//更新用户
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('user','lis'));
		$this->user = spClass('m_user')->spLinker()->find(array('id'=>$id));
		$this->display('user_upd.tpl.php');
	}
	public function doupd(){
		$rows = $this->spArgs();
		if(isset($rows['password2'])) unset($rows['password2']);
		if(isset($rows['usercode'])) unset($rows['usercode']);
		if(isset($rows['password']) && !empty($rows['password'])){
			$rows['password'] = md5($rows['password']);
		}else{
			unset($rows['password']);
		}
		$rows['kysj'] = strtotime($rows['kysj']);
		if(false != spClass('m_user')->update(array('id'=>$rows['id']),$rows)){
			$this->success('更新会员信息成功！',spUrl('user','lis'));
		}else{
			$this->error('更新会员信息失败！',spUrl('user','lis'));
		}
	}
	//删除用户
	public function del(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('user','lis'));
		if(false != spClass('m_user')->delete(array('id'=>$id))){
			$this->success('删除会员成功！',spUrl('user','lis'));
		}else{
			$this->error('删除会员失败！',spUrl('user','lis'));
		}
	}
	//切换状态
	public function updst(){
		if(!$idrow = $this->spArgs('id'))	$this->error('请选择要切换状态的编号！',spUrl('user','lis'));
		for($i=0;$i<count($idrow);$i++){
			$isnormal = spClass('m_user')->find(array('id'=>$idrow[$i]),null,'status');
			if($isnormal['status'] == 0){
				$isnormal = 1;
			}else{
				$isnormal = 0;
			}
			spClass('m_user')->update(array('id'=>$idrow[$i]),array('status'=>$isnormal));
		}
		$this->success('切换状态成功！',spUrl('user','lis'));
	}
	//检测账号唯一性
	public function checkcode(){
		if(!$usercode = trim($this->spArgs('usercode'))) exit;
		if(false != spClass('m_user')->find(array('usercode'=>$usercode))){
			exit('1'); //账号已存在
		}
		exit('0');
	}
}